import Layer from '../../core/Layer';
import Text from '../../base/Text';

export default class extends Layer {
    constructor(canvas, style) {
        super(canvas, style);
        this.formatter = style.formatter || (value => value);
        this.max = style.max || 0;
        this.min = style.min || 0;
        this.cols = style.cols || 5;
    }

    get height() {
        return parseInt(this.fontSize);
    }

    build(painter) {
        this.childs.splice(0);
        const { min, max, cols } = this;
        const xAxisStep = (max - min) / cols;
        const xStep = this.width / cols;
        for (let i = 0; i <= cols; i++) {
            const text = this.formatter(min + i * xAxisStep);
            const txt = new Text(this.canvas, {
                fontSize: this.fontSize,
                fontFamily: this.fontFamily,
                color: this.color,
                text,
                type: Text.TYPE.FILL,
                textAlign: 'center',
                position: 'absolute',
                left: i * xStep,
            });
            if (i === 0) {
                txt.textAlign = 'left'
            } else if (i === cols) {
                txt.textAlign = 'right';
            }
            this.addChild(txt);
        }
    }
}
